
jQuery(document).ready(
    function () {
		let url = location.search;
		let Request = new Object();
		Request = GetRequest(url);
    	initNav(Request.m);//加载顶部导航栏
    	initToolBar();//加载顶部工具栏
    	initSideBar(Request);//加载侧边栏
    	loadHtmlAndJs(Request);
    });
/*
//监听hash值变化，实现页面变换
window.onhashchange = function(){ 
　　var hash=window.location.hash;//获取hash值	
	let Request = new Object();
	Request = GetRequestByHash(hash);//将获取的hash值转化成对象
	initNav(Request.m);
	initSideBar(Request);
	loadHtmlAndJs(Request);//加载对应页面的html与JS
	
}*/
//加载顶部导航栏
function initNav(activeTab){
	//获取一下nav
	let nav =[{
		text:"首页",
		adress:"main.html?m=index&a=index",
		name:"index",

	},{
		text:"商品",
		adress:"main.html?m=item&a=index",
		name:"item",
	},{
		text:"订单",
		adress:"main.html?m=order&a=index",
		name:"order",
	}]
	activeTab = !activeTab?"index":activeTab;
	$("#nav ul").empty();
	$.each(nav,function(index,value){
		let html;
		if (value.name==activeTab){
			html = '<li class="active"><a href="'+value.adress+'">'+value.text+'</a></li>';
		}else {
			html = '<li><a href="'+value.adress+'">'+value.text+'</a></li>';
		}
		
		$("#nav ul").append(html);
	});
}
function initToolBar(){
	//获取一下nav
	let toolBar =[{
		name:"网站首页",
		adress:"main.html?m=index&a=index"
	},{
		name:"删除缓存",
		adress:""
	},{
		name:"退出",
		adress:""
	}]
	$.each(toolBar,function(index,value){
		let html = '<li><a href="'+value.adress+'">'+value.name+'</a></li>';
		$("#toolBar ul").append(html);
	});
}

//从链接中获取参数
function GetRequestByHash(url) {
	  var theRequest = new Object();
	  if (url.indexOf("#") != -1) {
	    var str = url.substr(1);
	    strs = str.split("&");
	    for(var i = 0; i < strs.length; i ++) {
	      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
	    }
	  }
	  return theRequest;
}
function GetRequest(url) {
	url = url?url:"?m=index&a=index";
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for(var i = 0; i < strs.length; i ++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}
function initSideBar(requeset){
	let side = {
		index:[{
			name:"系统信息",
			info:[{
				text:"基本信息",
				adress:"main.html?m=index&a=index",
				name:"index",
			},{
				text:"账号设置",
				adress:"main.html?m=index&a=account",
				name:"account",
			}],
		}],
		item:[{
			name:"商品",
			info:[{
				text:"商品列表",
				adress:"main.html?m=item&a=index",
				name:"index",
			},{
				text:"添加商品",
				adress:"main.html?m=item&a=edit",
				name:"edit",
			}],
		}],
		order:[{
			name:"订单",
			info:[{
				text:"所有订单",
				adress:"main.html?m=order&a=index",
				name:"index",
			},{
				text:"添加订单",
				adress:"main.html?m=order&a=add",
				name:"add",
			}],
		},{
			name:"订单统计",
			info:[{
				text:"订单统计",
				adress:"main.html?m=order&a=statistics",
				name:"statistics",
			},],
		},]
	}
	let activeSide;
	let activeTab;
	let activeSideTab;
	if (!requeset){
		activeTab = "index";
		activeSide = side["index"]
	}else {
		activeSide = side[requeset.m];
		activeTab = requeset.a;
	}
	$("#sideBar").empty();
	$.each(activeSide,function(index,value){
		let html;
		html='<div class="box sub-nav"><h3>'+value.name+'</h3><ul class="ui-list">';
		$.each(value.info,function(index2,value2){
			if (value2.name==activeTab){
				html += '<li class="active"><a href="'+value2.adress+'">'+value2.text+'</a></li>';
			}else {
				html += '<li><a href="'+value2.adress+'">'+value2.text+'</a></li>';
			}
		});
		html += '</ul></div>';
		
		$("#sideBar").append(html);
	});

}
function loadHtmlAndJs(Request){
	if (!Request){
		Request = {};
		Request.m="index";
		Request.a="index";
	}
	let hurl = "html/"+Request.m+"/"+Request.a+".html"
	let jsurl = "js/"+Request.m+"/"+Request.a+".js"
	$("#showBox").empty();
	$.ajax({
		url: hurl,
		cache:true,
		async:false,
		xhrFields: { withCredentials: true },
		success: function (html) {
			$("#showBox").html(html);
		}
	});

	loadScript(jsurl);	
}


//动态引入JS
function loadScript(url, callback) {

    var script = document.createElement("script");

    script.type = "text/javascript";

    if (typeof(callback) != "undefined") {

        if (script.readyState) {

            script.onreadystatechange = function() {

                if (script.readyState == "loaded" || script.readyState == "complete") {

                    script.onreadystatechange = null;

                    callback();

                }

            };

        } else {

            script.onload = function() {

                callback();

            };

        }

    };

    script.src = url;

    document.body.appendChild(script);

}

